package com.riche.codefactory.model.s2c;

import cn.hutool.json.JSONUtil;
import com.riche.codefactory.model.domain.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目视图
 *
 */
@Data
public class S2cQuestion implements Serializable {

    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 标签列表
     */
    private List<String> tagList;

    /**
     * 推荐答案
     */
    private String answer;

    /**
     * 创建用户信息
     */
    private S2cUser user;

    /**
     * 封装类转对象
     *
     * @param s2cQuestion
     * @return
     */
    public static Question voToObj(S2cQuestion s2cQuestion) {
        if (s2cQuestion == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(s2cQuestion, question);
        List<String> tagList = s2cQuestion.getTagList();
        question.setTags(JSONUtil.toJsonStr(tagList));
        return question;
    }

    /**
     * 对象转封装类
     *
     * @param question
     * @return
     */
    public static S2cQuestion objToVo(Question question) {
        if (question == null) {
            return null;
        }
        S2cQuestion s2cQuestion = new S2cQuestion();
        BeanUtils.copyProperties(question, s2cQuestion);
        s2cQuestion.setTagList(JSONUtil.toList(question.getTags(), String.class));
        return s2cQuestion;
    }
}
